package com.guli.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonUtils.R;
import com.guli.eduService.entity.EduCourse;
import com.guli.eduService.entity.EduTeacher;
import com.guli.eduService.entity.vo.TeacherQuery;
import com.guli.eduService.mapper.EduTeacherMapper;
import com.guli.eduService.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.serviceBase.Entity.GuliExcepiton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author zxc
 * @since 2023-03-26
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Resource
    public EduTeacherMapper eduTeacherMapper;

    @Autowired
    private EduCourseServiceImpl eduCourseService;

    @Override
    public boolean deleteById(String id) {
        EduTeacher eduTeacher = eduTeacherMapper.queryById(id);
        System.out.println(eduTeacher);
        if(eduTeacher != null) {
            return removeById(id);
        }
        return false;
    }

    @Override
    public void QueryThroughConditions(Page<EduTeacher> eduTeacherPage, TeacherQuery teacherQuery) {
        // QueryWrapper对象中，封装了所要查询条件;
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        // 若是teacherQuery，也就是前端所传递的查询条件为空的话，则直接返回即可;
        if(teacherQuery == null){
            baseMapper.selectPage(eduTeacherPage , queryWrapper);
            return;
        }
        // 前端页面所传递多来的条件查询数据，将其封装到Wrapper对象中;
        String teacherName = teacherQuery.getName();
        Integer teacherLevel = teacherQuery.getLevel();
        String teacherBegin = teacherQuery.getBegin();
        String teacherEnd = teacherQuery.getEnd();

        if(!StringUtils.isEmpty(teacherName)){
            queryWrapper.like("name" , teacherName);
        }
        if(!StringUtils.isEmpty(teacherLevel)){
            queryWrapper.eq("level" , teacherLevel);
        }
        if(!StringUtils.isEmpty(teacherBegin)){
            queryWrapper.ge("gmt_create" , teacherBegin);
        }
        if(!StringUtils.isEmpty(teacherEnd)){
            queryWrapper.le("gmt_modified" , teacherEnd);
        }
        // 所查询到符合条件的所有数据，都被封装到Page对象（分页组件）中
        eduTeacherMapper.selectPage(eduTeacherPage , queryWrapper);
    }

    // 测试 ：mapper文件中自定义sql语句，用来根据id来查询讲师数据：
    @Override
    public EduTeacher queryById(String id) {
        EduTeacher eduTeacher = baseMapper.queryById(id);
        if(eduTeacher == null){
            throw new GuliExcepiton(20001 , "该讲师不存在！！！");
        }
        return eduTeacher;
    }

    @Override
    @Cacheable(value = "teacher" , key = "'indexTeacherList'")
    public List<EduTeacher> queryByTeacherNum(String teacherNum) {
        // 2.封装讲师的查询规则
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("level");
        teacherQueryWrapper.last("limit " + teacherNum);
        List<EduTeacher> teacherInfo = this.list(teacherQueryWrapper);
        return teacherInfo;
    }

    @Override
    public Map<String, Object> getFrontTeacherInfoCondition(Page<EduTeacher> page) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        this.page(page , queryWrapper);
        List<EduTeacher> records = page.getRecords();
        long pages = page.getPages();
        long size = page.getSize();
        long current = page.getCurrent();
        long total = page.getTotal();
        boolean previous = page.hasPrevious();
        boolean next = page.hasNext();

        Map<String , Object> map = new HashMap<>();
        map.put("records" , records);
        map.put("pages" , pages);
        map.put("size" , size);
        map.put("total" , total);
        map.put("current" , current);
        map.put("previous" , previous);
        map.put("next" , next);

        return map;
    }

    @Override
    public List<EduCourse> getTeacherCourse(String teacherId) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id" , teacherId);
        List<EduCourse> courses = eduCourseService.list(queryWrapper);
        return courses;
    }
}
